package pimp.organizermanager;

import java.util.*;

public class Year {
    private HashMap<Integer, Month> months;
    private List<Event> events;
    private int thisYear;

    public Year(int year) {
        months = new HashMap<Integer, Month>();
        events = new ArrayList<Event>();
        thisYear = year;
    }

    public boolean addEvent(Event event, Date beginDate, Date endDate) {
        int beginYear = beginDate.getThisYear();
        int endYear = endDate.getThisYear();
        int beginMonth = beginDate.getThisMonth();
        int endMonth = endDate.getThisMonth();
        if(beginMonth == 0) {
            events.add(event);
            event.addYear(this);
        }
        else {
            if(beginYear == endYear) {
                for(int i = beginMonth; i <= endMonth; ++i) {
                    if(months.get(i) == null) {
                        months.put(i, new Month(i, thisYear));
                    }
                    Month month = months.get(i);
                    if(month.addEvent(event, beginDate, endDate) == false) {
                        return false;
                    }
                }
            }
            else {
               if(thisYear == beginYear) {
                   for(int i = beginMonth; i <= 12; ++i) {
                    if(months.get(i) == null) {
                        months.put(i, new Month(i, thisYear));
                    }
                    Month month = months.get(i);
                    if(month.addEvent(event, beginDate, endDate) == false) {
                        return false;
                    }
                }
               }
               else if(thisYear == endYear) {
                   for(int i = 1; i <= endMonth; ++i) {
                    if(months.get(i) == null) {
                        months.put(i, new Month(i, thisYear));
                    }
                    Month month = months.get(i);
                    if(month.addEvent(event, beginDate, endDate) == false) {
                        return false;
                    }
                }
               }
               else {
                   for(int i = 1; i <= 12; ++i) {
                    if(months.get(i) == null) {
                        months.put(i, new Month(i, thisYear));
                    }
                    Month month = months.get(i);
                    if(month.addEvent(event, beginDate, endDate) == false) {
                        return false;
                    }
                }
               }
            }
        }
        return true;
    }

    public Map<ID, GlobalEvent> getGlobalEventByDate(Date beginDate, Date endDate,
            Map<ID, GlobalEvent> map) {
        int beginYear = beginDate.getThisYear();
        int endYear = endDate.getThisYear();
        int beginMonth = beginDate.getThisMonth();
        int endMonth = endDate.getThisMonth();
        if(map == null) {
            map = new HashMap<ID, GlobalEvent>();
        }
        if(beginYear != endYear) {
            if(thisYear == beginYear) {
                for(int i = beginMonth; i <= 12; ++i) {
                Month month = months.get(i);
                if(month != null) {
                    month.getGlobalEventByDate(beginDate, endDate, map);
                }
                }
            }
            else if (thisYear == endYear) {
                for(int i = 1; i <= endMonth; ++i) {
                    Month month = months.get(i);
                    if(month != null) {
                        month.getGlobalEventByDate(beginDate, endDate, map);
                    }
                }
            }
            else {
                for(int i = 1; i <= 12; ++i) {
                    Month month = months.get(i);
                    if(month != null) {
                        month.getGlobalEventByDate(beginDate, endDate, map);
                    }
                }
            }
        }
        else {
            for(int i = beginMonth; i<= endMonth; ++i) {
                Month month = months.get(i);
                if(month != null) {
                    month.getGlobalEventByDate(beginDate, endDate, map);
                }
            }
        }
        for(int i = 0; i < events.size(); ++i) {
            Event event = events.get(i);
            map.put(event.getId(), event);
        }
        return map;
    }
    public String toString()
    {
        String out = new String();
        return out;
    }
}
